import TileLayer from "ol/layer/Tile"
import TileArcGISRest from 'ol/source/TileArcGISRest'
import OSM from "ol/source/OSM"
import XYZ from 'ol/source/XYZ'
import TileGrid from 'ol/tilegrid/TileGrid'
import WMS from 'ol/source/TileWMS'
// import WMTS from 'ol/source/WMTS'
// import WMTSTileGrid from 'ol/tilegrid/WMTS'

import proj4 from 'proj4'
import {get as getProjection } from "ol/proj"
// import { getWidth, getTopLeft } from "ol/extent"
import { register } from 'ol/proj/proj4'

let maptype = 3
var streetMap = function() {
    var mapLayer = [];
    switch (maptype) {
        case 0:
            mapLayer = [new TileLayer({
                source: new XYZ({
                    url: 'http://127.0.0.1:7080/streetmap/shenzhen/{z}/{x}/{y}.jpg'
                })
            })];
            break;
        case 1:
            mapLayer = [new TileLayer({
                source: new OSM()
            })];
            break;
        case 2:
            mapLayer = [new TileLayer({
                source: new TileArcGISRest({
                    url: 'https://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineCommunity/MapServer'
                })
            })];
            break;
        case 3:
            var projection = getProjection('EPSG:4326');
            // var projectionExtent = projection.getExtent();
            // var size = getWidth(projectionExtent) / 256;
            // var resolutions = new Array(18);
            // var matrixIds = new Array(18);
            // matrixIds[0] = 0;
            // for (var z = 1; z < 22; ++z){ // 计算比例尺
            //     resolutions[z] = size / Math.pow(2, z);
            //     matrixIds[z] = z;
            // }
            // debugger;
            var webKey = '2d8eca459ee67a4b8d1f281791d4f09f';
            // var tiandituVurl = 'http://t{0-7}.tianditu.gov.cn/vec_c/wmts?tk='; //底图
            // var tiandituCurl = 'http://t{0-7}.tianditu.gov.cn/cva_c/wmts?tk='; //注记
            mapLayer = [
                // new TileLayer({
                //     source: new WMTS({
                //         url: tiandituVurl + webKey,
                //         layer:'vec',
                //         matrixSet:'c',
                //         format: 'tiles',
                //         style: 'default',
                //         projection: projection,
                //         tileGrid: new WMTSTileGrid({
                //             origin: getTopLeft(projectionExtent),
                //             resolutions: resolutions,
                //             matrixIds: matrixIds
                //         }),
                //         wrapX:true
                //     })
                // }),
                // new TileLayer({
                //     source: new WMTS({
                //         url: tiandituCurl + webKey,
                //         layer: 'cva',
                //         matrixSet: 'c',
                //         format: 'tiles',
                //         style: 'default',
                //         projection: projection,
                //         tileGrid: new WMTSTileGrid({
                //             origin: getTopLeft(projectionExtent),
                //             resolutions: resolutions,
                //             matrixIds: matrixIds
                //         }),
                //         wrapX:true
                //     })
                // })

                // 影像地图
                new TileLayer({
                    source: new XYZ({
                        url: 'http://t' + Math.round(Math.random() * 7) + '.tianditu.gov.cn/img_c/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=c&FORMAT=tiles&TILECOL={x}&TILEROW={y}&TILEMATRIX={z}&tk=' + webKey,
                        projection: projection
                    })
                }),
                // 影像注记
                new TileLayer({
                    source: new XYZ({
                        url: 'http://t' + Math.round(Math.random() * 7) + '.tianditu.gov.cn/cia_c/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cia&STYLE=default&TILEMATRIXSET=c&FORMAT=tiles&TILECOL={x}&TILEROW={y}&TILEMATRIX={z}&tk=' + webKey,
                        projection: projection
                    })
                })
                // 矢量地图
                // new TileLayer({
                //     source: new XYZ({
                //         url: 'http://t'+Math.round(Math.random()*7)+'.tianditu.gov.cn/vec_c/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=c&FORMAT=tiles&TILECOL={x}&TILEROW={y}&TILEMATRIX={z}&tk=' + webKey,
                //         projection: projection
                //     })
                // }),
                // 矢量注记
                // new TileLayer({
                //     source: new XYZ({
                //         url: 'http://t'+Math.round(Math.random()*7)+'.tianditu.gov.cn/cva_c/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cva&STYLE=default&TILEMATRIXSET=c&FORMAT=tiles&TILECOL={x}&TILEROW={y}&TILEMATRIX={z}&tk=' + webKey,
                //         projection: projection
                //     })
                // })
            ]
            break;
        default:
            break;
    }
    return mapLayer
}

var add4490 = function() {
    var resolutions4490 = [
        1.40625, // Level 0
        0.703125, // Level 1
        0.3515625, // Level 2
        0.17578125, // Level 3
        0.087890625, // Level 4
        0.0439453125, // Level 5
        0.02197265625,
        0.010986328125,
        0.0054931640625,
        0.00274658203125,
        0.001373291015625,
        6.866455078125E-4,
        3.4332275390625E-4,
        1.71661376953125E-4,
        8.58306884765625E-5,
        4.291534423828125E-5,
        2.1457672119140625E-5,
        1.0728836059570312E-5,
        5.364418029785156E-6,
        2.682209014892578E-6,
        1.341104507446289E-6,
        6.705522537231445E-7,
        3.3527612686157227E-7 //22
    ];
    var tilegrid4490 = new TileGrid({
        tileSize: 256,
        origin: [-180, 90],
        resolutions: resolutions4490
    })
    proj4.defs('EPSG:4490', '+proj=longlat +ellps=GRS80 +no_defs');
    register(proj4);
    var layer4490 = new TileLayer({
        source: new XYZ({
            url: 'https://zrzyhghj2.jiaxing.gov.cn/arcgis/rest/services/JXSKY/JXIMG/MapServer/tile/{z}/{y}/{x}?blankTile=false?token=e6871e660b76ab0ee63abd3b21c20f8b',
            projection: 'EPSG:4490',
            tileGrid: tilegrid4490
        }),
        minZoom: 17,
        maxZoom: 22
    })
    return layer4490
}

var mapConfig = {
    // x: 120.759129, //中心点经度和纬度 嘉兴
    // y: 30.752432,
    // x: 119.9127832, //中心点经度和纬度
    // y: 30.352478027,
    x: 121.01606, //中心点经度和纬度 平湖市
    y: 30.67585,
    zoom: 16, //地图缩放级别
    minZoom: 1,
    maxZoom: 21,
    streetmap: streetMap,
    layer4490: add4490
}

export default mapConfig